import { merge } from '../../helper/helper';


const defaultState = {
    workflowList: {
        isPending: false,
        skip: 0,
        count: 0,
        chosen: [0, 1, 2],
        orderBy: 'createTimestamp',
        data: [],
        currentPageIndex: 0,
        targetPageIndex: 0,
        direction: 'DESC'
    },
    newWorkflow: {
        isAddWorkflow: false
    },
    historyList: {
        isPending: false,
        skip: 0,
        count: 0,
        chosen: [0, 1, 2],
        orderBy: 'time',
        data: [],
        currentPageIndex: 0,
        targetPageIndex: 0,
        direction: 'DESC'
    },
    detail: {
        currentId: '',
        detailInfo: { name: '' }
    }
}


function reducer(state = defaultState, action) {
    switch (action.type) {
        case 'workflow-list-render-data':
            return merge(state, {
                workflowList: merge(state.workflowList, {
                    isPending: false,
                    data: action.val.data,
                    count: action.val.total
                })
            })
        case 'workflow-list-request':
            return merge(state, {
                workflowList: merge(state.workflowList, merge(action.val, {
                    isPending: true
                }))
            });
        case 'workflow-list-set-setPageIndex':
            return merge(state, {
                workflowList: merge(state.workflowList, {
                    currentPageIndex: action.val,
                    targetPageIndex: action.val
                })
            });
        case 'workflow-list-set-chosen':
            return merge(state, {
                workflowList: merge(state.workflowList, {
                    chosen: action.val
                })
            });
        case 'trigger-add-task':
            console.log(action.val,'okoko')
            if (!action.val) {
                return merge(state, {
                    newWorkflow: {
                        isAddWorkflow: !state.AddWorkflow.isAddWorkflow,

                    }
                })
            } else {
                var data = action.val,
                    typeIndex = typeList.findIndex(item => item.name == data.type),
                    userIndex = state.users.findIndex(item => item.name == data.owner);
                return merge(state, {
                    newTask: merge(state.newTask, {
                        isAddTask: !state.newTask.isAddTask,
                        modalType: 'edit',
                        title: data.title,
                        owner: data.owner,
                        typeIndex: typeIndex == -1 ? 0 : typeIndex,
                        userIndex: userIndex,
                        userId: state.users[userIndex].id,

                    })
                })
            }
        case 'history-list-render-data':
            return merge(state, {
                historyList: merge(state.historyList, {
                    isPending: false,
                    data: action.val.data,
                    count: action.val.total
                })
            })
        case 'history-list-request':
            return merge(state, {
                historyList: merge(state.historyList, merge(action.val, {
                    isPending: true
                }))
            });
        case 'history-list-set-setPageIndex':
            return merge(state, {
                historyList: merge(state.historyList, {
                    currentPageIndex: action.val,
                    targetPageIndex: action.val
                })
            });
        case 'set-current-id':
            return merge(state, {
                detail: merge(state.detail, {
                    currentId: action.val
                })
            })
        case 'set-detail-info':
            return merge(state, {
                detail: merge(state.detail, {
                    detailInfo: action.val
                })
            })
    }
    return state;
}

export default reducer;